﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_VendorChange : CommonPageBase
    {
        List<TempFile> files = new List<TempFile>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBaseHelper.BindDrp(Drp_VDTYPE, EnumData.DataBaseEunm.VenType);
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                LoadData();
                BindGrid();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Proj_Vendor Ven = TbsDbSession.Default.From<Proj_Vendor>().Where(Proj_Vendor._.ID == int.Parse(Request["id"])).ToFirst();
                if (Ven != null)
                {
                    Drp_VDTYPE.SelectedValue = Ven.VDTYPE;
                    txt_DEPTON.Text = Ven.DEPTON;
                    txt_ADDRES.Text = Ven.ADDRES;
                    txt_BAKNAM.Text = Ven.BAKNAM;
                    txt_BAKNUB.Text = Ven.BAKNUB;
                    txt_TAXNUB.Text = Ven.TAXNUB;
                    txt_CONTAT.Text = Ven.CONTAT;
                    txt_TELPHO.Text = Ven.TELPHO;
                    txt_UEMAIL.Text = Ven.UEMAIL;
                    hf.Text = Ven.ATTACH;
                    txt_ElseTel.Text = Ven.ElseTel;
                    hid_VEDNUB.Text = Ven.VEDNUB;
                    txt_EasyName.Text = Ven.EasyName;
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool combin = false;
            int returnvalue = 0;
            Proj_Vendor Ven = new Proj_Vendor();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                string H = StringHelper.GetSubStringList(Drp_VDTYPE.SelectedValue, '－')[1];
                Ven.ID = int.Parse(Request["id"]);
                Ven.Attach();
                Ven.VEDNUB = H + hid_VEDNUB.Text.Substring(1, hid_VEDNUB.Text.Length - 1);
                Ven.DEPTON = txt_DEPTON.Text;
                Ven.ADDRES = txt_ADDRES.Text;
                Ven.BAKNAM = txt_BAKNAM.Text;
                Ven.BAKNUB = txt_BAKNUB.Text;
                Ven.TAXNUB = txt_TAXNUB.Text;
                Ven.CONTAT = txt_CONTAT.Text;
                Ven.TELPHO = txt_TELPHO.Text;
                Ven.UEMAIL = txt_UEMAIL.Text;
                Ven.ATTACH = hf.Text;
                Ven.ElseTel = txt_ElseTel.Text;
                Ven.EasyName = txt_EasyName.Text;
                Ven.VDTYPE = Drp_VDTYPE.SelectedValue;
                returnvalue = TbsDbSession.Default.Update<Proj_Vendor>(Ven);
                TbsDbSession.Default.FromSql("update Proj_Podata set VEDNUB='" + Ven.VEDNUB + "' where VEDNUB='" + hid_VEDNUB.Text + "'").ExecuteNonQuery();
            }
            else
            {
                Ven.DeAttach();
                string H = StringHelper.GetSubStringList(Drp_VDTYPE.SelectedValue, '－')[1];
                Ven.VEDNUB = H + DFieldSN.SN(EnumData.DataSNEunm.供应商编号, false);
                Ven.VDTYPE = Drp_VDTYPE.SelectedValue;
                Ven.DEPTON = txt_DEPTON.Text;
                Ven.ADDRES = txt_ADDRES.Text;
                Ven.BAKNAM = txt_BAKNAM.Text;
                Ven.BAKNUB = txt_BAKNUB.Text;
                Ven.TAXNUB = txt_TAXNUB.Text;
                Ven.CONTAT = txt_CONTAT.Text;
                Ven.TELPHO = txt_TELPHO.Text;
                Ven.UEMAIL = txt_UEMAIL.Text;
                Ven.Adder = CookInfo.EAdminTrueName();
                Ven.AddID = CookInfo.EAdminid();
                Ven.AddTime = DateTime.Now;
                Ven.ATTACH = hf.Text;
                Ven.ElseTel = txt_ElseTel.Text;
                Ven.EasyName = txt_EasyName.Text;
                returnvalue = TbsDbSession.Default.Insert<Proj_Vendor>(Ven);
            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
            }
        }


        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (filePhoto.HasFile)
            {
                string fileName = filePhoto.ShortFileName;
                String[] str = { "1", "2", "3", "4", "5", "6", "7" };
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                filePhoto.SaveAs(Server.MapPath("~/upload/" + fileName));
                hf.Text += fileName + ";";
                // 清空文件上传组件
                filePhoto.Reset();
                BindGrid();
            }
        }

        protected void BindGrid()
        {
            List<string> ls = StringHelper.GetSubStringList(hf.Text, ';');
            foreach (string item in ls)
            {
                TempFile tf = new TempFile();
                tf.FileName = item;
                files.Add(tf);
            }
            Grid1.DataSource = files;
            Grid1.DataBind();
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string[] str = Grid1.Rows[e.RowIndex].Values;
                hf.Text = hf.Text.Replace(str[1], "");
                BindGrid();
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[0] = "<a href=\"/upload/" + e.Values[0] + "\">" + e.Values[0] + "</a>";
        }


    }
}